package com.tros.customer.features;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tros.customer.notification.MailNotification;

/**
 * Servlet implementation class PayTransfer
 */
@WebServlet("/PayTransfer")
public class PayTransfer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PayTransfer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Connection connect =null;			 
		HttpSession ses = request.getSession();
		
		String accType = request.getParameter("accType");
		String accNo = request.getParameter("accno");
		String balance = request.getParameter("balAmt");
		String billAmt = request.getParameter("billAmt");
		String billaccNo = request.getParameter("BilaccNo");
		String service = request.getParameter("serOption");
		String billType = request.getParameter("payType");
		
		
		System.out.println("accType :"+accType);
		System.out.println("balance :"+balance);
		System.out.println("billAmt :"+billAmt);
		System.out.println("service :"+service);
		System.out.println("billaccNo :"+billaccNo);
		System.out.println("accNo :"+accNo);
		
		try{
			  System.out.println("--------------> balance -------> "+Double.parseDouble(balance));
			  System.out.println("--------------> billAmt -------> "+Double.parseDouble(billAmt)); 
			  
	           if(Double.parseDouble(balance) < Double.parseDouble(billAmt))
	           {
	        	   System.out.println("came here ------->balance less");
	        	   ses.setAttribute("passPayResult", "Payment Amount Entered Exceeds Account Balance");
			       response.sendRedirect("passPayResult.jsp");
			       
	           }else
	           {
	        	   System.out.println("came here ------->balance ok");
	        	   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			       connect =DriverManager.getConnection("jdbc:odbc:bank","root", " ");
		           System.out.println("DB Connection created :"+connect);
		           
	        	   Double bal = Double.parseDouble(balance)- Double.parseDouble(billAmt);
		           System.out.println("----------> amount ---------->"+bal);
		           
		           
		        	   DetectAcc(connect,accType,accNo,bal,ses);
		        	   
		        	   ses.setAttribute("passPayResult", "Bill Payment Successfully Done");
				       response.sendRedirect("passPayResult.jsp");
				       
				       try{
							
							 UpdateTrans trans = new UpdateTrans(connect,accNo,accType,"Bill Payment",billAmt,String.valueOf(ses.getAttribute("currentID")));
							 trans.updateTransValues();
							 
							 						 
						}catch(Exception e)
						{
							e.printStackTrace();
							System.out.println(" -----------> Failed in updation trans --------");
						}
				       
				       try{
							
				    	     UpdateBillPay trans = new UpdateBillPay(connect,accNo,accType,billType,billAmt,service,String.valueOf(ses.getAttribute("currentID")));
							 trans.updateTransValues();							 
							 						 
						}catch(Exception e)
						{
							e.printStackTrace();
							System.out.println(" -----------> Failed in updation trans --------");
						}     
				         
						
						try{
							
							String email = "";
							String lname = "";
							
							PreparedStatement state=connect.prepareStatement("SELECT * FROM EXISTUSERS WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));
							ResultSet result=state.executeQuery();
							
							while(result.next()){
								
							      email=result.getString("EMAIL");
							      lname=result.getString("LASTNAME");
							}
							
							state.close();
							
							MailNotification mail = new MailNotification(email);
						    
						    String text = "Hello "+ lname + 
							    		      "\n\n Bill Payment is been performed in your"+
							    		      "\n\n Account : "+accType+
							    		      "\n Number : "+accNo+
							    		      "\n Payment Amount : "+billAmt+
							    		       "\n Payee Bill : "+billType+
							    		        "\n Payee Service : "+service+
							    		      "\n\n Note : Please do not reply to this E-Mail Notification";
							    
							mail.sendMail( "Cash Deposited", text);
							connect.close();
							
						}catch(Exception e){
							e.printStackTrace();
							System.out.println(" -----------> Failed in Mail notification --------");
						}
		         
	           }
	           
		}catch(Exception e)
		{
			ses.setAttribute("passPayResult", "Amount Payment Failure \n Please Try again");
	        response.sendRedirect("passPayResult.jsp");
			System.out.println("Failed here --------->"+e.getMessage());
	       	e.printStackTrace();
		}
		
		
	}

	private void DetectAcc(Connection connect, String accType, String accNo,
			Double bal, HttpSession ses) {
		// TODO Auto-generated method stub
		
		try{
			PreparedStatement state;
			
			  if(accType.equals("Savings")){				  
				  state=connect.prepareStatement( "UPDATE SAVACC SET ACC_BALANCE=? WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));            
			  }else
			  {				  
				   state=connect.prepareStatement( "UPDATE CHCKACC SET ACC_BALANCE=? WHERE "+"CUSTOMERID="+ses.getAttribute("currentID"));	           
			  }
			  
			state.setString(1,String.valueOf(bal));
	   	    state.executeUpdate();
	   	    state.close();
	  		System.out.println("---------> connect value ------"+connect);
			
		}catch(Exception e)
		{
			System.out.println("Failed here update amount --------->");
			e.printStackTrace();
		}
		
	}

}
